package Work;

public class CreatStack extends Stack{
    Object[] arr;
    int count;
    int capacity = 4 ;

    public CreatStack(){
        arr = new Object[capacity];
    }

    public CreatStack(int capacity){
        if (capacity < 0){
            capacity = 10;
        }
        this.capacity = capacity;
        arr = new Object[capacity];
    }
    @Override
    //进栈
    void push(Object element) {
        grow();
        arr[count] = element;
        count ++;
    }

    @Override
    Object pop() {
        Object remove = arr[count - 1];
            System.arraycopy(arr, 0, arr, 0, count - 1);
            System.arraycopy(arr, count, arr, count - 1, arr.length - count);
            count--;
            return remove;
    }

    @Override
    Object peek() {
        if (count  ==  0) {
           return null;
        }else {
            return arr[count - 1];
        }
    }

    @Override
    boolean isEmpty() {
        if (count == 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    int size() {
        return count;
    }

    @Override
    void grow() {
        if (count >= arr.length) {
            Object[] newArr = new Object[capacity * 2];
            System.arraycopy(arr, 0, newArr, 0, arr.length);
            arr = newArr;
        }
    }


}
